package com.droidclaimcustomer;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.droidclaimcustomer.R;
import com.droidclaimcustomer.activity.ClaimDetailActivity;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gms.maps.model.LatLng;
import com.google.gson.Gson;

public class GCMIntentService extends GCMBaseIntentService {

	private static final String TAG = "GCMIntentService";
	private final String CODEMAP = "map";
	private final String CODEACCEPTJOB = "acceptjob";
	private final String CODEUPDATEJOB = "updatejob";
	static final String SENDER_ID = "344698691373";
	public static String registrationId = "";
	
    public static final String DISPLAY_MAP_ACTION =
            "com.droidclaimcustomer.DISPLAY_MAP";
    public static final String EXTRA_MAP = "map";
	

	public GCMIntentService() {
		super(SENDER_ID);
	}

	@Override
	protected void onError(Context arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onMessage(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub
		Log.e(TAG + "Message", "Got an onMessage!");
		Log.e(TAG + "Message", arg0.toString() + arg1.toString());
		String message = arg1.getExtras().getString("message");
		String code = arg1.getExtras().getString("code");
		if(code.trim().equals(CODEMAP))
		{
			// Set lat long ToMap Activity Map
			// message = { "lat":10.123123 , "lng":123.203023 }
			displayMessage(arg0, message);
			
		}
		else if(code.trim().equals(CODEACCEPTJOB) || code.trim().equals(CODEUPDATEJOB))
		{
			generateNotification(arg0, message);
		}
		Log.i("Message", message);

	}

	@Override
	protected void onRegistered(Context arg0, String arg1) {
		Log.e(TAG + "Registration", "Got an onRegistered!");
		Log.e(TAG + "Registration", arg0.toString() + arg1.toString());
		registrationId = arg1;
		// TODO Auto-generated method stub

	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	private static void generateNotification(Context context, String message) {
		int icon = R.drawable.ic_launcher;
		long when = System.currentTimeMillis();
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, "Job Detail", when);

		String title = context.getString(R.string.app_name);

		// message = { "id":1 }
		// request_id = 103
		AcceptId claimInfoId = new Gson().fromJson(message, AcceptId.class);
		Intent notificationIntent = new Intent(context,
				ClaimDetailActivity.class);
		notificationIntent.putExtra("request_id", "103");
		notificationIntent.putExtra("id", String.valueOf(claimInfoId.getId()));
		
		// set intent so it does not start a new activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(context, title, "Job Detail", intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		// Play default notification sound
		notification.defaults |= Notification.DEFAULT_SOUND;

		// notification.sound = Uri.parse("android.resource://" +
		// context.getPackageName() + "your_sound_file_name.mp3");

		// Vibrate if vibrate is enabled
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notificationManager.notify(0, notification);

	}

    static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MAP_ACTION);
        intent.putExtra(EXTRA_MAP, message);
        context.sendBroadcast(intent);
    }
    private class AcceptId{
    	private int id;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}
    }
}
